package com.hlmc.spider;
import com.hlmc.config.Constants;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import javax.annotation.Resource;
import java.util.List;

@Service("ds_360spider")
public class DS360_dt_spider extends BaseProcessor {

    @Resource
    private DS_pipeline ds_pipeline;

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100).setCharset("utf-8" + "").setTimeOut(8000);

    @Override
    public void process(Page page) {

        page.addTargetRequests(page.getHtml().xpath("//dd[@class='item g-clear js-filter-content']/a[@class='js-tongjip']/@href").all());

        String str="https://www.360kan.com/dianshi/list.php?cat=";

        //String str="https://www.360kan.com/dianshi/list.php?rank=rankhot&cat=all&area=all&act=all&year=all&pageno";
        if(page.getUrl().toString().contains(str))
        {
            page.addTargetRequests( page.getHtml().css("#js-ew-page a:last-child","href").all());

            page.addTargetRequests(page.getHtml().xpath("//div[@class='s-tab-main']/ul/li/a/@href").all());
        }


        String model_="//div[@class='s-top g-clear s-top-dianshi']";
        String model_r="//div[@class='s-top g-clear s-top-dianshi']/div[@class='s-top-right']/div[@class='s-top-info']";
       // List<String> list = page.getHtml().links().regex("(https://www.360kan.com/tv/\\w+.html)").all();
      //  page.addTargetRequests(list);

        if(page.getUrl().regex("https://www.360kan.com/tv/\\w{14}\\.html").match()){

            String ds_title=page.getHtml().xpath(model_r+"/div[1]/div[@class='title-left g-clear']/h1/text()").toString().replace(" ","");
            String image= page.getHtml().xpath(model_+"/div[@class='s-top-left']/div/a/img/@src").toString();
            String score=page.getHtml().xpath(model_r+"/div[1]/div[@class='s-title-right']/span/text()").toString();
            String toup=page.getHtml().xpath(model_r+"/div[1]/div/p/html()").toString().replace("<span>","").replace("</span>","");;
            String type=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[1]/a/text()").all().toString().replace("[","").replace("]","");
            String year=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[2]/text()").toString().replace("[","").replace("]","");
            String area=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[3]/text()").toString().replace("[","").replace("]","");
            String director=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[5]/a/text()").toString().replace("[","").replace("]","");
            String actor=page.getHtml().xpath("//div[@id='js-desc-switch']/div[1]/p[6]/a/text()").all().toString().replace("[","").replace("]","");
            String detail=page.getHtml().xpath("//div[@id='js-desc-switch']/div[3]/p/text()").toString().replace("[","").replace("]","");
            String player_url_1=page.getHtml().xpath("//div[@class='num-tab js-tabs']/div[2]/a/@href").all().toString().replace("[","").replace("]","").replace(" ", "");
            String player_url_2=page.getHtml().xpath("//div[@class='num-tab js-tabs']/div/a/@href").all().toString().replace("[","").replace("]","").replace(" ", "");
            //System.out.println(ds_title);
            String player_url=player_url_1;
            if(player_url_1==null||player_url_1.equals("")){
                player_url=player_url_2;
            }
            if(image!=null&&ds_title!=null&&!ds_title.equals("")&&!image.equals("")) {

                page.putField("title", ds_title);
                page.putField("toup", toup);
                page.putField("image", image);
                page.putField("score", score);
                page.putField("type",type);
                page.putField("year",year);
                page.putField("area",area);
                page.putField("director",director);
                page.putField("actor",actor);
                page.putField("detail",detail);
                page.putField("purl",player_url);
                page.putField("category", Constants.CATEGORY_1);

            }

           //System.out.println( page.getHtml().css("#js-ew-page a:last-child","href"));

            if( (page.getResultItems().get("title") == null)||(page.getResultItems().get("title").toString()).replace(" ", "").equals("")) {

                page.setSkip(true);
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {

        Spider.create(new DS360_dt_spider()).addUrl("https://www.360kan.com/dianshi/list.php?rank=rankhot&cat=all&area=all&act=all&year=all&pageno=1")
                .addPipeline(new DS_pipeline())
                .thread(5).run();

    }
}
